@@ -180,11 +180,13 @@ defmodule Nx do
180180 ]
181181 >
182182
183- Negative positions in ranges read from the back. The right-side of
184- the range must be equal or greater than the left-side:
183+ Ranges can receive negative positions and they will read from
184+ the back. In such cases, the range step must be explicitly given
185+ and the right-side of the range must be equal or greater than
186+ the left-side:
185187
186188 iex> t = Nx.tensor([[1, 2], [3, 4], [5, 6], [7, 8]])
187- iex> t[1..-2]
189+ iex> t[1..-2//1 ]
188190 #Nx.Tensor<
189191 s64[2][2]
190192 [
@@ -198,7 +200,7 @@ defmodule Nx do
198200 axes with ranges, it is often desired to use a list:
199201
200202 iex> t = Nx.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
201- iex> t[[1..-2, 1..2]]
203+ iex> t[[1..-2//1 , 1..2]]
202204 #Nx.Tensor<
203205 s64[2][2]
204206 [
@@ -210,14 +212,14 @@ defmodule Nx do
210212 You can mix both ranges and integers in the list too:
211213
212214 iex> t = Nx.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
213- iex> t[[1..-2, 2]]
215+ iex> t[[1..-2//1 , 2]]
214216 #Nx.Tensor<
215217 s64[2]
216218 [6, 9]
217219 >
218220
219221 If the list has less elements than axes, the remaining dimensions
220- are returned in full (equivalent to 0..-1) :
222+ are returned in full:
221223
222224 iex> t = Nx.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
223225 iex> t[[1..2]]
@@ -352,7 +354,7 @@ defmodule Nx do
352354 [44, 45, 46]
353355 >
354356
355- Mixed types get the highest precision type :
357+ Mixed types give higher priority to floats :
356358
357359 iex> Nx.tensor([1, 2, 3.0])
358360 #Nx.Tensor<
@@ -398,9 +400,24 @@ defmodule Nx do
398400 ]
399401 >
400402
403+ Besides single-precision (32 bits), floats can also have
404+ half-precision (16) or double-precision (64):
405+
406+ iex> Nx.tensor([1, 2, 3], type: {:f, 16})
407+ #Nx.Tensor<
408+ f16[3]
409+ [1.0, 2.0, 3.0]
410+ >
411+
412+ iex> Nx.tensor([1, 2, 3], type: {:f, 64})
413+ #Nx.Tensor<
414+ f64[3]
415+ [1.0, 2.0, 3.0]
416+ >
417+
401418 Brain-floating points are also supported, although they are
402419 emulated in Elixir and therefore perform slower without a
403- compilation backend:
420+ native backend:
404421
405422 iex> Nx.tensor([1, 2, 3], type: {:bf, 16})
406423 #Nx.Tensor<
@@ -2319,7 +2336,7 @@ defmodule Nx do
23192336
23202337 """
23212338 @ doc type: :shape
2322- def size ( shape ) when is_tuple ( shape ) , do: tuple_product ( shape , tuple_size ( shape ) )
2339+ def size ( shape ) when is_tuple ( shape ) , do: Tuple . product ( shape )
23232340 def size ( tensor ) , do: size ( shape ( tensor ) )
23242341
23252342 @ doc """
@@ -2363,10 +2380,6 @@ defmodule Nx do
23632380 defp count_up ( 0 , _n ) , do: [ ]
23642381 defp count_up ( i , n ) , do: [ n | count_up ( i - 1 , n + 1 ) ]
23652382
2366- # TODO: Use Tuple.product on Elixir v1.12
2367- defp tuple_product ( _tuple , 0 ) , do: 1
2368- defp tuple_product ( tuple , i ) , do: :erlang . element ( i , tuple ) * tuple_product ( tuple , i - 1 )
2369-
23702383 ## Backend API
23712384
23722385 @ backend_key { Nx , :default_backend }
@@ -7992,13 +8005,10 @@ defmodule Nx do
79928005 defp to_indices ( start_indices ) do
79938006 all_static? = Enum . all? ( start_indices , & is_integer / 1 )
79948007
7995- # TODO: Use Enum.with_index/3 in Elixir v1.12
79968008 if all_static? do
79978009 start_indices
79988010 else
7999- start_indices
8000- |> Enum . with_index ( )
8001- |> Enum . map ( fn { index , i } ->
8011+ Enum . with_index ( start_indices , fn index , i ->
80028012 % T { shape: idx_shape , type: idx_type } = t = to_tensor ( index )
80038013
80048014 unless idx_shape == { } do
0 commit comments