Skip to content

Commit

Permalink
RF backend make_output_tensor, TF-layers disallow transpose
Browse files Browse the repository at this point in the history
Fix #1410
  • Loading branch information
albertz committed Oct 6, 2023
1 parent 206bbea commit 6ae497b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
13 changes: 13 additions & 0 deletions returnn/frontend/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ def transpose(tensor: Tensor, perm: Sequence[Union[Dim, int]], *, allow_int: boo
out.raw_tensor = backend.transpose_raw(tensor.raw_tensor, perm_)
return out

@staticmethod
def make_output_tensor(tensor: Tensor, dims: Sequence[Dim], *, name: str) -> Tensor:
"""
:param tensor:
:param dims:
:param name:
:return: tensor with dims order like in dims
"""
# noinspection PyProtectedMember
tensor = tensor._raw_backend.transpose(tensor, dims, allow_int=False)
tensor = tensor.copy(name=name)
return tensor

@staticmethod
def expand_dims_raw(raw_tensor: T, axis: int) -> T:
"""
Expand Down
4 changes: 2 additions & 2 deletions returnn/frontend/run_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ def mark_as_output(self, tensor: Union[Tensor, Any], name: str, *, dims: Optiona
# We try some reasonable defaults, specifically: BTF or BF.
dims = _default_dim_order(tensor)
assert set(dims) == set(tensor.dims), f"mark_as_output: tensor {tensor} does not have the dims {dims}"
tensor = tensor.copy_transpose(dims, allow_int=False)
tensor = tensor.copy(name=name)
# noinspection PyProtectedMember
tensor = tensor._raw_backend.make_output_tensor(tensor, dims, name=name)
assert name not in self.outputs.data
self.outputs.data[name] = tensor

Expand Down
8 changes: 6 additions & 2 deletions returnn/tf/frontend_layers/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ def reshape_raw(raw_tensor: Layer, shape: Union[Sequence[Union[int, Layer]], Lay
@staticmethod
def transpose(tensor: Tensor, perm: Sequence[Union[Dim, int]], *, allow_int: bool = False) -> Tensor:
"""transpose"""
assert all(isinstance(d, Dim) for d in perm) # axis as int not supported
return rfl.make_layer({"class": "transpose", "from": tensor, "perm": perm}, name="transpose")
raise Exception("TF-layers backend: order of dims is irrelevant")

@staticmethod
def make_output_tensor(tensor: Tensor, dims: Sequence[Dim], *, name: str) -> Tensor:
"""only func where we have explicitly defined dim order in the output"""
return rfl.make_layer({"class": "transpose", "from": tensor, "perm": dims}, name=name)

@staticmethod
def copy(tensor: Tensor) -> Tensor:
Expand Down

0 comments on commit 6ae497b

Please sign in to comment.