<commit_before>// Copyright 2020 The SwiftFusion Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import PenguinStructures

extension AnyArrayBuffer where Dispatch == VectorArrayDispatch {
  /// Creates an instance from an untyped buffer with a dispatcher that is at least as specific as
  /// `Dispatch`.
  ///
  /// This initializer is effectively an up-cast.
  init<D: VectorArrayDispatch>(_ src: AnyArrayBuffer<D>) {
    self.init(unsafelyCasting: src)
  }
}

extension AnyArrayBuffer: Equatable where Dispatch: VectorArrayDispatch {
  /// Returns `true` iff `lhs` and `rhs` are equivalent tensors.
  ///
  /// - Requires: `lhs.tensorShapeIsCompatible(withThatOf: rhs)`.
  public static func ==(lhs: Self, rhs: Self) -> Bool {
    lhs.dispatch.equals(.init(lhs), .init(rhs))
  }
}

extension AnyArrayBuffer where Dispatch: VectorArrayDispatch
{
  /// Returns the elementwise sum of `lhs` and `rhs`
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func + (_ lhs: Self, _ rhs: Self) -> Self {
    .init(unsafelyCasting: lhs.dispatch.sum(lhs.upcast, rhs.upcast))
  }
  
  /// Returns the elementwise difference of `lhs` and `rhs`.
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func - (_ lhs: Self, _ rhs: Self) -> Self {
    .init(unsafelyCasting: lhs.dispatch.difference(lhs.upcast, rhs.upcast))
  }

  /// Accumulates the elements of rhs into those of lhs via addition.
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func += (_ lhs: inout Self, _ rhs: Self) {
    lhs.dispatch.add(&lhs.upcast, rhs.upcast)
  }
  
  /// Accumulates the elements of rhs into those of lhs via subtraction.
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func -= (_ lhs: inout Self, _ rhs: Self) {
    lhs.dispatch.subtract(&lhs.upcast, rhs.upcast)
  }
}

extension AnyArrayBuffer: AdditiveArithmetic where Dispatch == VectorArrayDispatch
{
  public static var zero: Self { .init(ArrayBuffer<Vector1>()) }
}

extension AnyArrayBuffer: Differentiable where Dispatch: DifferentiableArrayDispatch {
  public typealias TangentVector = AnyVectorArrayBuffer

  public mutating func move(along direction: TangentVector) {
    dispatch.move(&self.upcast, direction.upcast)
  }

  public var zeroTangentVectorInitializer: () -> TangentVector {
    { .init(ArrayBuffer<Vector1>()) }
  }
}<commit_msg>Closer to Vector<commit_after>// Copyright 2020 The SwiftFusion Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import PenguinStructures

extension AnyArrayBuffer where Dispatch == VectorArrayDispatch {
  /// Creates an instance from an untyped buffer with a dispatcher that is at least as specific as
  /// `Dispatch`.
  ///
  /// This initializer is effectively an up-cast.
  init<D: VectorArrayDispatch>(_ src: AnyArrayBuffer<D>) {
    self.init(unsafelyCasting: src)
  }
}

extension AnyArrayBuffer: Equatable where Dispatch: VectorArrayDispatch {
  /// Returns `true` iff `lhs` and `rhs` are equivalent tensors.
  ///
  /// - Requires: `lhs.tensorShapeIsCompatible(withThatOf: rhs)`.
  public static func ==(lhs: Self, rhs: Self) -> Bool {
    lhs.dispatch.equals(.init(lhs), .init(rhs))
  }
}

extension AnyArrayBuffer where Dispatch: VectorArrayDispatch {
  /// Returns the elementwise sum of `lhs` and `rhs`
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func + (_ lhs: Self, _ rhs: Self) -> Self {
    .init(unsafelyCasting: lhs.dispatch.sum(lhs.upcast, rhs.upcast))
  }
  
  /// Returns the elementwise difference of `lhs` and `rhs`.
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func - (_ lhs: Self, _ rhs: Self) -> Self {
    .init(unsafelyCasting: lhs.dispatch.difference(lhs.upcast, rhs.upcast))
  }

  /// Accumulates the elements of rhs into those of lhs via addition.
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func += (_ lhs: inout Self, _ rhs: Self) {
    lhs.dispatch.add(&lhs.upcast, rhs.upcast)
  }
  
  /// Accumulates the elements of rhs into those of lhs via subtraction.
  ///
  /// - Requires: the arguments have elements of the same type.
  public static func -= (_ lhs: inout Self, _ rhs: Self) {
    lhs.dispatch.subtract(&lhs.upcast, rhs.upcast)
  }

  static func *= (_ lhs: inout Self, _ rhs: Double) {
    lhs.dispatch.scale(&lhs.upcast, rhs)
  }

  static func * (_ lhs: Double, _ rhs: Self) -> Self {
    .init(unsafelyCasting: rhs.dispatch.scaled(rhs.upcast, lhs))
  }
}

extension AnyArrayBuffer: AdditiveArithmetic where Dispatch == VectorArrayDispatch
{
  public static var zero: Self { .init(ArrayBuffer<Vector1>()) }
}

extension AnyArrayBuffer: Differentiable where Dispatch: DifferentiableArrayDispatch {
  public typealias TangentVector = AnyVectorArrayBuffer

  public mutating func move(along direction: TangentVector) {
    dispatch.move(&self.upcast, direction.upcast)
  }

  public var zeroTangentVectorInitializer: () -> TangentVector {
    { .init(ArrayBuffer<Vector1>()) }
  }
}
{'fraction_non_alphanumeric': 0.11201298701298701, 'fraction_numerical': 0.003246753246753247, 'mean_word_length': 3.4228284278535535, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1889857', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1829, 'n_words': 637}