<commit_before># Copyright 2017-2018 <NAME> & the Arraymancer contributors
#
# 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  typetraits,
        ../tensor/tensor,
        ./autograd_common

template `[]`*[TT](v: Variable[TT], args: varargs[untyped]): Variable[TT] =
  ## Slice the tensor contained by the dynamic graph Variable
  ## Input:
  ##   - a Variable
  ## Output:
  ##   - a sliced Variable

  # TODO - investigate https://github.com/mratsim/Arraymancer/issues/241
  # As https://github.com/mratsim/Arraymancer/commit/e609e998d663710281dbe161249a0139befa818c
  # which fixed https://github.com/mratsim/Arraymancer/issues/185 had to be rollbacked

  # Ensure that v is only called once even if it's a function with side-effects
  let z = v

  # TODO: backprop support
  var result: type(z)
  new result

  type S = type z.value[args]

  result.context = z.context
  result.requires_grad = z.requires_grad
  when S is AnyTensor:
    result.value = z.value[args]
    result.grad = z.grad[args]
  else: # Not sure how to backprop that
    result.value = [z.value[args]].toTensor
    result.grad = [z.grad[args]].toTensor

  result

  # TODO: tests for slicing correspondence

# #############################################

type ReshapeGate*{.final.}[TT] = ref object of Gate[TT]
  cached_input_shape: MetadataArray
<commit_msg>Fix slicing of Variables that don't require a gradient<commit_after># Copyright 2017-2018 <NAME> & the Arraymancer contributors
#
# 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  typetraits,
        ../tensor/tensor,
        ./autograd_common

template `[]`*[TT](v: Variable[TT], args: varargs[untyped]): Variable[TT] =
  ## Slice the tensor contained by the dynamic graph Variable
  ## Input:
  ##   - a Variable
  ## Output:
  ##   - a sliced Variable

  # TODO - investigate https://github.com/mratsim/Arraymancer/issues/241
  # As https://github.com/mratsim/Arraymancer/commit/e609e998d663710281dbe161249a0139befa818c
  # which fixed https://github.com/mratsim/Arraymancer/issues/185 had to be rollbacked

  # Ensure that v is only called once even if it's a function with side-effects
  let z = v

  # TODO: backprop support
  var result: type(z)
  new result

  type S = type z.value[args]

  result.context = z.context
  result.requires_grad = z.requires_grad
  when S is AnyTensor:
    result.value = z.value[args]
    if result.requires_grad:
      result.grad = z.grad[args]
  else: # Not sure how to backprop that
    result.value = [z.value[args]].toTensor
    if result.requires_grad:
      result.grad = [z.grad[args]].toTensor

  result

  # TODO: tests for slicing correspondence

# #############################################

type ReshapeGate*{.final.}[TT] = ref object of Gate[TT]
  cached_input_shape: MetadataArray

{'fraction_non_alphanumeric': 0.11822269240941549, 'fraction_numerical': 0.024332187252049723, 'mean_word_length': 3.7572327044025156, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '5314026', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1124, 'n_words': 390}