~/bend-docscommunity

src/tensor/transform_LAWS.bend source

src/tensor/transform_LAWS.bend on the hub · documented module

import Baseimport ../shape/core.bend as Shapeimport ./layout.bend as Layoutimport ./core.bend as Coreimport ./transforms.bend as Transformsimport ../storage/main.bend as Storageimport ../storage/model.bend as Modelimport ../storage/refinement.bend as Refinementimport ./transform_totality.bend as Totalityimport ./transpose_spec.bend as Transposelaw permutation_checked:  for +input: Shape.Shape  for +axes: List<&2, U32>  for plan: Layout.Plan<input, axes>  {True{} == Layout.bijective(input, axes) : Bool}law permutation_dimensions:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  {Some{Shape.dimensions(Layout.output(input, axes, plan))} == Layout.reorder(Shape.dimensions(input), axes) : Maybe<&2, List<&2, U32>>}law permutation_count:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat}law mapped_inverse:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -i: U32  for +mapping: Layout.Mapping<input, axes, plan, i>  {Some{Layout.input_words(input, axes, plan, i, mapping)} ==    Layout.inverse_coordinates(Shape.dimensions(input), axes, Layout.output_words(input, axes, plan, i, mapping), 0) : Maybe<&2, List<&2, U32>>}law mapped_flat:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -i: U32  for +mapping: Layout.Mapping<input, axes, plan, i>  {Layout.source_index(input, axes, plan, i, mapping) ==    Layout.flatten_words(Shape.dimensions(input), Layout.input_words(input, axes, plan, i, mapping), 0) : U32}law mapped_output:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -i: U32  for mapping: Layout.Mapping<input, axes, plan, i>  {Layout.output_words(input, axes, plan, i, mapping) ==    Layout.reverse_words(Layout.unflatten_words(Layout.reverse_words(Shape.dimensions(Layout.output(input, axes, plan))), i)) : List<&2, U32>}law mapped_bounded:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -i: U32  for mapping: Layout.Mapping<input, axes, plan, i>  {True{} == U32.is_lt(Layout.source_index(input, axes, plan, i, mapping), Shape.count(input)) : Bool}law inverse_coordinate_roundtrip:  for -input: Shape.Shape  for +axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -i: U32  for +mapping: Layout.Mapping<input, axes, plan, i>  {Layout.reorder(Layout.input_words(input, axes, plan, i, mapping), axes) ==    Some{Layout.output_words(input, axes, plan, i, mapping)} : Maybe<&2, List<&2, U32>>}law reshape_linear_order:  for +input: Shape.Shape  for +target: Shape.Shape  for tensor: Core.Tensor<input>  for +index: U32  for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat}  {Transforms.dynamic_observe(Transforms.reshape_equal(input, target, tensor, same), index) ==    Core.observe(input, tensor, index) : Maybe<&2, F32>}law reshape_target_dimensions:  for +input: Shape.Shape  for +target: Shape.Shape  for tensor: Core.Tensor<input>  for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat}  {Transforms.dynamic_dimensions(Transforms.reshape_equal(input, target, tensor, same)) == Shape.dimensions(target) : List<&2, U32>}law copy_input_preservation:  for +depth: Nat  for array: Array<F32>  for +index: U32  for +other: U32  for balanced: Model.Balanced(F32, depth, array)  {Model.observe(F32, depth, Refinement.first(F32, Array.get(F32, array, index)), Refinement.route(depth, other)) ==    Model.observe(F32, depth, array, Refinement.route(depth, other)) : Maybe<&2, F32>}law copy_write_frame:  for +depth: Nat  for array: Array<F32>  for +index: U32  for +other: U32  for +value: F32  for balanced: Model.Balanced(F32, depth, array)  {Model.observe(F32, depth, Array.set(F32, array, index, value), Refinement.route(depth, other)) ==    Model.select(F32, Model.same(depth, Refinement.route(depth, index), Refinement.route(depth, other)), value,      Model.observe(F32, depth, array, Refinement.route(depth, other))) : Maybe<&2, F32>}law zero_count_skips_copy:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for source: Core.Tensor<input>  for target: Core.Tensor<Layout.output(input, axes, plan)>  for +cursor: Nat  for empty: {Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) == 0n : Nat}  {Transforms.copy_loop(Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))), input, axes, plan, source, target, cursor) ==    (source, Done{target}) : Core.Tensor<input> & Result<&2, &1, Transforms.Error, Core.Tensor<Layout.output(input, axes, plan)>>}law natural_cursor_checked:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -cursor: Nat  for result: Transforms.NaturalCopy<input, axes, plan, cursor>  Transforms.CursorValid(input, axes, plan, cursor, result)law copy_source_mapping:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -output_index: U32  for -mapping: Layout.Mapping<input, axes, plan, output_index>  for -source: Core.Tensor<input>  for certificate: Core.Read<input, source, Layout.source_index(input, axes, plan, output_index, mapping)>  {Core.read_value(input, Core.read_flat(input, source, Layout.source_index(input, axes, plan, output_index, mapping))) ==    Core.certificate_value(input, source, Layout.source_index(input, axes, plan, output_index, mapping), certificate) : Maybe<&2, F32>}law primitive_source_mapping:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -output_index: U32  for +mapping: Layout.Mapping<input, axes, plan, output_index>  for +depth: Nat  for array: Array<F32>  for balanced: Model.Balanced(F32, depth, array)  {Some{Refinement.second(F32, Array.get(F32, array, Layout.source_index(input, axes, plan, output_index, mapping)))} ==    Model.observe(F32, depth, array, Refinement.route(depth, Layout.source_index(input, axes, plan, output_index, mapping))) : Maybe<&2, F32>}law copy_value_mapping:  for -input: Shape.Shape  for -axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +depth: Nat  for +length: Nat  for array: Array<F32>  for +index: U32  for source: Core.Tensor<input>  for +value: F32  for balanced: Model.Balanced(F32, depth, array)  for -valid: {Storage.bounds(depth, length) == True{} : Bool}  for -exact: {length == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat}  for allowed: {Nat.is_lt(U32.to_nat(index), length) == True{} : Bool}  for matched: {Storage.index_matches(depth, length, index) == True{} : Bool}  {Transforms.copy_observe(input, axes, plan,    Transforms.copy_read(input, axes, plan, index, source => target => (source, Done{target}),      Core.TTensor{Storage.Owned{depth, length, array, balanced, valid}, exact}, (source, Done{value})), index) == Some{value} : Maybe<&2, F32>}law checked_copy_step_mapping:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +index: U32  for +mapping: Layout.Mapping<input, axes, plan, index>  for map_exact: {Done{mapping} == Layout.map_index(input, axes, plan, index) : Result<&2, &2, Layout.Error, Layout.Mapping<input, axes, plan, index>>}  for -source: Core.Tensor<input>  for certificate: Core.Read<input, source, Layout.source_index(input, axes, plan, index, mapping)>  for +depth: Nat  for +length: Nat  for array: Array<F32>  for balanced: Model.Balanced(F32, depth, array)  for -valid: {Storage.bounds(depth, length) == True{} : Bool}  for -exact: {length == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat}  for allowed: {Nat.is_lt(U32.to_nat(index), length) == True{} : Bool}  for matched: {Storage.index_matches(depth, length, index) == True{} : Bool}  {Transforms.copy_observe(input, axes, plan,    Transforms.copy_step(input, axes, plan, source,      Core.TTensor{Storage.Owned{depth, length, array, balanced, valid}, exact}, index), index) ==    Core.certificate_value(input, source, Layout.source_index(input, axes, plan, index, mapping), certificate) : Maybe<&2, F32>}law checked_copy_source_meaning:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +index: U32  for +mapping: Layout.Mapping<input, axes, plan, index>  for map_exact: {Done{mapping} == Layout.map_index(input, axes, plan, index) : Result<&2, &2, Layout.Error, Layout.Mapping<input, axes, plan, index>>}  for source: Core.Tensor<input>  for +depth: Nat  for +length: Nat  for array: Array<F32>  for balanced: Model.Balanced(F32, depth, array)  for -valid: {Storage.bounds(depth, length) == True{} : Bool}  for -exact: {length == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat}  for allowed: {Nat.is_lt(U32.to_nat(index), length) == True{} : Bool}  for matched: {Storage.index_matches(depth, length, index) == True{} : Bool}  Transforms.CopyMeaning<input, axes, plan, index, mapping, source,    Core.TTensor{Storage.Owned{depth, length, array, balanced, valid}, exact}>law initial_prefix:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -source: Core.Tensor<input>  for -target: Core.Tensor<Layout.output(input, axes, plan)>  Transforms.Prefix<input, axes, plan, source, source, target, 0n>law advance_prefix:  for -input: Shape.Shape  for -axes: List<&2, U32>  for -plan: Layout.Plan<input, axes>  for -original: Core.Tensor<input>  for -before_source: Core.Tensor<input>  for -after_source: Core.Tensor<input>  for -before_target: Core.Tensor<Layout.output(input, axes, plan)>  for -after_target: Core.Tensor<Layout.output(input, axes, plan)>  for -cursor: Nat  for prefix: Transforms.Prefix<input, axes, plan, original, before_source, before_target, cursor>  for advanced: Transforms.Advanced<input, axes, plan, original, before_source, after_source, before_target, after_target, cursor>  Transforms.Prefix<input, axes, plan, original, after_source, after_target, 1n+cursor>law whole_copy_interpretation:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for source: Core.Tensor<input>  for target: Core.Tensor<Layout.output(input, axes, plan)>  Transforms.WholeCopyModel<input, axes, plan, source, target>law permute_result_interpretation:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +axes: List<&2, U32>  Transforms.PublicModel<input, axes, source, Transforms.permute_copy(input, source, axes)>law transpose_result_interpretation:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +a: U32  for +b: U32  Transforms.PublicModel<input, Layout.transpose_axes(Shape.dimensions(input), a, b, 0), source,    Transforms.transpose_copy(input, source, a, b)>law reshape_result_interpretation:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +dimensions: List<&2, U32>  Transforms.ReshapeModel<input, source, dimensions>law reshape_inverse_observations:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +dimensions: List<&2, U32>  Transforms.ReshapeInverseModel<input, source, dimensions>law mapping_reference:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +index: U32  for mapping: Layout.Mapping<input, axes, plan, index>  {Some{Layout.source_index(input, axes, plan, index, mapping)} == Layout.reference_source(input, axes, plan, index) : Maybe<&2, U32>}law mapped_mathematical_bounded:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +index: U32  for mapping: Layout.Mapping<input, axes, plan, index>  {Nat.is_lt(U32.to_nat(Layout.source_index(input, axes, plan, index, mapping)),    Shape.logical_numel(Shape.dimensions(input))) == True{} : Bool}law natural_word_exact:  for +number: Nat  for +limit: U32  for +within: {Nat.is_le(number, U32.to_nat(limit)) == True{} : Bool}  {U32.to_nat(U32.from_nat(number)) == number : Nat}law codec_flatten_decode:  for +shape: Shape.Shape  for +index: U32  for +inside: {True{} == Nat.is_lt(U32.to_nat(index), Shape.elements(shape)) : Bool}  {Layout.flatten_words(Shape.dimensions(shape),    Layout.reverse_words(Layout.unflatten_words(Layout.reverse_words(Shape.dimensions(shape)), index)), 0) == index : U32}law reshape_valid_success:  for +input: Shape.Shape  for +target: Shape.Shape  for source: Core.Tensor<input>  for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat}  Transforms.ReshapeSuccessModel<input, target, source>law reshape_inverse_valid_success:  for +input: Shape.Shape  for +target: Shape.Shape  for source: Core.Tensor<input>  for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat}  Transforms.ReshapeInverseSuccessModel<input, target, source>law permutation_valid_success:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +valid: {True{} == Layout.bijective(input, axes) : Bool}  Totality.PlanSuccess<input, axes>law tensor_read_valid_success:  for +shape: Shape.Shape  for source: Core.Tensor<shape>  for +index: U32  for -inside: {Nat.is_lt(U32.to_nat(index), Shape.logical_numel(Shape.dimensions(shape))) == True{} : Bool}  Totality.ReadSuccess<shape, source, index>law tensor_write_valid_success:  for +shape: Shape.Shape  for target: Core.Tensor<shape>  for +index: U32  for +value: F32  for -inside: {Nat.is_lt(U32.to_nat(index), Shape.logical_numel(Shape.dimensions(shape))) == True{} : Bool}  Totality.WriteSuccess<shape, target, index, value>law decoded_words_valid_success:  for +shape: Shape.Shape  for +index: U32  for +inside: {True{} == Nat.is_lt(U32.to_nat(index), Shape.elements(shape)) : Bool}  Totality.WordsSuccess<Shape.dimensions(shape),    Layout.reverse_words(Layout.unflatten_words(Layout.reverse_words(Shape.dimensions(shape)), index))>law permute_axes_length_rejection:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +axes: List<&2, U32>  for -wrong: {False{} == Nat.is_eq(Shape.rank_dimensions(axes), Shape.rank_dimensions(Shape.dimensions(input))) : Bool}  Totality.PermuteFailure<input, axes, source, Layout.AxesLengthMismatch{}>law permute_axis_bounds_rejection:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +axes: List<&2, U32>  for -ranked: {True{} == Nat.is_eq(Shape.rank_dimensions(axes), Shape.rank_dimensions(Shape.dimensions(input))) : Bool}  for -outside: {False{} == Layout.bounded(axes, Shape.rank(input)) : Bool}  Totality.PermuteFailure<input, axes, source, Layout.AxisOutOfBounds{}>law permute_duplicate_axes_rejection:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +axes: List<&2, U32>  for -ranked: {True{} == Nat.is_eq(Shape.rank_dimensions(axes), Shape.rank_dimensions(Shape.dimensions(input))) : Bool}  for -bounded: {True{} == Layout.bounded(axes, Shape.rank(input)) : Bool}  for -duplicate: {False{} == Layout.unique(axes) : Bool}  Totality.PermuteFailure<input, axes, source, Layout.DuplicateAxis{}>law transpose_axis_bounds_rejection:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +a: U32  for +b: U32  for -outside: {False{} == Transpose.valid(input, a, b) : Bool}  Totality.TransposeFailure<input, source, a, b>law reshape_element_count_rejection:  for +input: Shape.Shape  for +target: Shape.Shape  for source: Core.Tensor<input>  for -different: {False{} == Nat.is_eq(Shape.logical_numel(Shape.dimensions(input)), Shape.logical_numel(Shape.dimensions(target))) : Bool}  Totality.ReshapeFailure<input, target, source>law mapping_index_rejection:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +index: U32  for +outside: {False{} == U32.is_lt(index, Shape.count(Layout.output(input, axes, plan))) : Bool}  {Layout.map_index(input, axes, plan, index) == Fail{Layout.ShapeError{Shape.FlatIndexOutOfBounds{}}} :    Result<&2, &2, Layout.Error, Layout.Mapping<input, axes, plan, index>>}law codec_decode_flatten:  for +shape: Shape.Shape  for +values: List<&2, U32>  for +valid: Layout.WordsValid(Shape.dimensions(shape), values)  {Layout.reverse_words(Layout.unflatten_words(Layout.reverse_words(Shape.dimensions(shape)),    Layout.flatten_words(Shape.dimensions(shape), values, 0))) == values : List<&2, U32>}law mapping_valid_success:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +index: U32  for +inside: {True{} == U32.is_lt(index, Shape.count(Layout.output(input, axes, plan))) : Bool}  Totality.MappingSuccess<input, axes, plan, index>law natural_cursor_exact_conversion:  for +shape: Shape.Shape  for +cursor: Nat  for +within: {Nat.is_le(cursor, Shape.elements(shape)) == True{} : Bool}  Transforms.CursorExact<cursor, U32.from_nat(cursor)>law copy_step_valid_success:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for source: Core.Tensor<input>  for target: Core.Tensor<Layout.output(input, axes, plan)>  for +index: U32  for +inside: {True{} == U32.is_lt(index, Shape.count(Layout.output(input, axes, plan))) : Bool}  Totality.CopyStepSuccess<input, axes, plan, source, target, index>law natural_step_valid_success:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +cursor: Nat  for source: Core.Tensor<input>  for target: Core.Tensor<Layout.output(input, axes, plan)>  for +inside: {True{} == Nat.is_lt(cursor, Shape.elements(Layout.output(input, axes, plan))) : Bool}  Totality.NaturalSuccess<input, axes, plan, cursor, Transforms.natural_step(input, axes, plan, cursor, source, target)>law copy_loop_valid_success:  for +fuel: Nat  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +cursor: Nat  for source: Core.Tensor<input>  for target: Core.Tensor<Layout.output(input, axes, plan)>  for +within: {Nat.is_le(Nat.add(fuel, cursor), Shape.elements(Layout.output(input, axes, plan))) == True{} : Bool}  Totality.CopySuccess<input, axes, plan, Transforms.copy_loop(fuel, input, axes, plan, source, target, cursor)>law permute_valid_success:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +axes: List<&2, U32>  for +valid: {True{} == Layout.bijective(input, axes) : Bool}  Totality.PublicSuccess<input, Transforms.permute_copy(input, source, axes)>law inverse_axes_valid:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  {True{} == Layout.bijective(Layout.output(input, axes, plan), Totality.inverse_axes(input, axes)) : Bool}law inverse_permutation_dimensions:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +inverse: Layout.Plan<Layout.output(input, axes, plan), Totality.inverse_axes(input, axes)>  {Shape.dimensions(Layout.output(Layout.output(input, axes, plan), Totality.inverse_axes(input, axes), inverse)) == Shape.dimensions(input) : List<&2, U32>}law inverse_mapping_restores_index:  for +input: Shape.Shape  for +axes: List<&2, U32>  for +plan: Layout.Plan<input, axes>  for +inverse: Layout.Plan<Layout.output(input, axes, plan), Totality.inverse_axes(input, axes)>  for +index: U32  for +second: Layout.Mapping<Layout.output(input, axes, plan), Totality.inverse_axes(input, axes), inverse, index>  for +first: Layout.Mapping<input, axes, plan,    Layout.source_index(Layout.output(input, axes, plan), Totality.inverse_axes(input, axes), inverse, index, second)>  {Layout.source_index(input, axes, plan,    Layout.source_index(Layout.output(input, axes, plan), Totality.inverse_axes(input, axes), inverse, index, second), first) == index : U32}law permute_typed_success:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +axes: List<&2, U32>  for +valid: {True{} == Layout.bijective(input, axes) : Bool}  Totality.TypedPermutation<input, axes, source, Transforms.permute_copy(input, source, axes)>law permute_inverse_restoration:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +axes: List<&2, U32>  for +valid: {True{} == Layout.bijective(input, axes) : Bool}  Totality.PermutationInverse<input, axes, source>law transpose_valid_success:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +a: U32  for +b: U32  for +inside: {True{} == Transpose.valid(input, a, b) : Bool}  Totality.PublicSuccess<input, Transforms.transpose_copy(input, source, a, b)>law transpose_requested_result:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +a: U32  for +b: U32  for +inside: {True{} == Transpose.valid(input, a, b) : Bool}  Totality.RequestedTranspose<input, source, a, b>law transpose_inverse_restoration:  for +input: Shape.Shape  for source: Core.Tensor<input>  for +a: U32  for +b: U32  for +inside: {True{} == Transpose.valid(input, a, b) : Bool}  Totality.TransposeInverse<input, source, a, b>