Skip to content

Commit

Permalink
#14179: Formating conv2d results.
Browse files Browse the repository at this point in the history
Co-authored-by: Lewis Panos <[email protected]>
  • Loading branch information
shwetankTT and LPanosTT committed Dec 6, 2024
1 parent 876aff0 commit 009aecf
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 72 deletions.
8 changes: 6 additions & 2 deletions models/demos/convnet_mnist/tt/convnet_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def convnet_mnist(
)

x = ttnn.to_layout(input_tensor, layout=ttnn.ROW_MAJOR_LAYOUT)
[x, out_height, out_width, weights_device, bias_device] = ttnn.conv2d(
x = ttnn.conv2d(
input_tensor=x,
weight_tensor=parameters.conv1.weight,
in_channels=1,
Expand All @@ -50,6 +50,8 @@ def convnet_mnist(
conv_op_cache={},
debug=True,
groups=1,
return_output_dim=False,
return_weights_and_bias=False,
)
x = ttnn.relu(x)

Expand All @@ -76,7 +78,7 @@ def convnet_mnist(
dilation=[1, 1],
)

[x, out_height, out_width, weights_device, bias_device] = ttnn.conv2d(
x, [out_height, out_width] = ttnn.conv2d(
input_tensor=x,
weight_tensor=parameters.conv2.weight,
in_channels=32,
Expand All @@ -93,6 +95,8 @@ def convnet_mnist(
conv_op_cache={},
debug=False,
groups=1,
return_output_dim=True,
return_weights_and_bias=False,
)

x = ttnn.relu(x)
Expand Down
4 changes: 3 additions & 1 deletion models/demos/segformer/tt/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __call__(self, device, input_tensor):
if self.act_block_h is not None:
conv_config.act_block_h_override = self.act_block_h

[output_tensor, _out_height, _out_width, self.weights, self.bias] = ttnn.conv2d(
[output_tensor, [_out_height, _out_width]] = ttnn.conv2d(
input_tensor=input_tensor,
weight_tensor=self.weights,
bias_tensor=self.bias,
Expand All @@ -72,6 +72,8 @@ def __call__(self, device, input_tensor):
input_width=input_tensor.shape[2],
conv_config=conv_config,
groups=self.groups,
return_output_dim=True,
return_weights_and_bias=False,
)

return output_tensor, _out_height, _out_width
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def run_downsample_if_req(
shard_layout = (
ttnn.TensorMemoryLayout.HEIGHT_SHARDED if height_sharding else ttnn.TensorMemoryLayout.BLOCK_SHARDED
)
ds_out, _, _, self.ds_conv_weight_tensor, self.ds_conv_bias_tensor = ttnn.conv2d(
ds_out, [self.ds_conv_weight_tensor, self.ds_conv_bias_tensor] = ttnn.conv2d(
input_tensor=x,
weight_tensor=self.ds_conv_weight_tensor,
in_channels=self.ds_conv_input_channels,
Expand All @@ -190,6 +190,8 @@ def run_downsample_if_req(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_output_dim=False,
return_weights_and_bias=True,
)
ttnn.deallocate(x)
ds_out = ttnn.reallocate(ds_out)
Expand All @@ -214,7 +216,7 @@ def __call__(
# conv1 is 1x1 conv
# print("Running conv1")
module_input_height = input_height
out, input_height, input_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
out, [input_height, input_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=x,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -238,6 +240,8 @@ def __call__(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)

act_block_h_override = 0
Expand Down Expand Up @@ -277,7 +281,7 @@ def __call__(
)
# if ds_out_mem_config and ds_out_mem_config != ttnn.get_memory_config(out):
# out = ttnn.to_memory_config(out, ds_out_mem_config)
out, input_height, input_width, self.conv2_weight_tensor, self.conv2_bias_tensor = ttnn.conv2d(
out, [input_height, input_width], [self.conv2_weight_tensor, self.conv2_bias_tensor] = ttnn.conv2d(
input_tensor=out,
weight_tensor=self.conv2_weight_tensor,
in_channels=self.conv2_input_channels,
Expand All @@ -304,11 +308,13 @@ def __call__(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)

# conv3 is 1x1 conv
# print("Running conv3")
out, _, _, self.conv3_weight_tensor, self.conv3_bias_tensor = ttnn.conv2d(
out, [self.conv3_weight_tensor, self.conv3_bias_tensor] = ttnn.conv2d(
input_tensor=out,
weight_tensor=self.conv3_weight_tensor,
in_channels=self.conv3_input_channels,
Expand All @@ -331,6 +337,8 @@ def __call__(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_weights_and_bias=True,
return_output_dim=False,
)

if not self.run_downsample_before_conv2:
Expand Down Expand Up @@ -546,7 +554,7 @@ def first_run(self, input_tensor, device, batch_size, ops_parallel_config) -> tt
input_tensor, device=device, memory_config=self.grayskull_conv1_input_memory_config
)

x, x_height, x_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
x, [x_height, x_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=input_tensor,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -569,6 +577,8 @@ def first_run(self, input_tensor, device, batch_size, ops_parallel_config) -> tt
act_block_h_override=act_block_h_override,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)
# Relu is fused with conv1

Expand Down Expand Up @@ -857,7 +867,7 @@ def optimized_run(self, input_tensor, device, batch_size, ops_parallel_config, c
input_tensor, device=device, memory_config=self.grayskull_conv1_input_memory_config
)

x, x_height, x_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
x, [x_height, x_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=input_tensor,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -880,6 +890,8 @@ def optimized_run(self, input_tensor, device, batch_size, ops_parallel_config, c
act_block_h_override=act_block_h_override,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)
# Relu is fused with conv1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def run_downsample_if_req(
):
if self.downsample:
logger.debug(f"Running downsample")
ds_out, _, _, self.ds_conv_weight_tensor, self.ds_conv_bias_tensor = ttnn.conv2d(
ds_out, [self.ds_conv_weight_tensor, self.ds_conv_bias_tensor] = ttnn.conv2d(
input_tensor=x,
weight_tensor=self.ds_conv_weight_tensor,
in_channels=self.ds_conv_input_channels,
Expand Down Expand Up @@ -195,6 +195,8 @@ def run_downsample_if_req(
enable_subblock_padding=enable_subblock_padding,
),
conv_op_cache=conv_op_cache,
return_output_dim=False,
return_weights_and_bias=True,
)
ttnn.deallocate(x)
ds_out = ttnn.reallocate(ds_out)
Expand Down Expand Up @@ -226,7 +228,7 @@ def __call__(
# conv1 is 1x1 conv
logger.debug(f"Running conv1")
module_input_height = input_height
out, input_height, input_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
out, [input_height, input_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=x,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -252,6 +254,8 @@ def __call__(
packer_l1_accum_enabled=packer_l1_acc,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)

act_block_h_override = 0
Expand Down Expand Up @@ -307,7 +311,7 @@ def __call__(

reallocate_halo_output = batch_size == 20
logger.debug(f"Running conv2")
out, input_height, input_width, self.conv2_weight_tensor, self.conv2_bias_tensor = ttnn.conv2d(
out, [input_height, input_width], [self.conv2_weight_tensor, self.conv2_bias_tensor] = ttnn.conv2d(
input_tensor=out,
weight_tensor=self.conv2_weight_tensor,
in_channels=self.conv2_input_channels,
Expand Down Expand Up @@ -340,6 +344,8 @@ def __call__(
enable_subblock_padding=enable_subblock_padding,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)

logger.debug(
Expand All @@ -358,7 +364,7 @@ def __call__(

# conv3 is 1x1 conv
logger.debug(f"Running conv3")
out, _, _, self.conv3_weight_tensor, self.conv3_bias_tensor = ttnn.conv2d(
out, [self.conv3_weight_tensor, self.conv3_bias_tensor] = ttnn.conv2d(
input_tensor=out,
weight_tensor=self.conv3_weight_tensor,
in_channels=self.conv3_input_channels,
Expand All @@ -383,6 +389,8 @@ def __call__(
packer_l1_accum_enabled=packer_l1_acc,
),
conv_op_cache=conv_op_cache,
return_output_dim=False,
return_weights_and_bias=True,
)

if not run_downsample_before_conv2:
Expand Down Expand Up @@ -719,7 +727,7 @@ def run(self, input_tensor, device, ops_parallel_config, conv_op_cache={}) -> tt
logger.debug(f"==== first conv")

# first conv
x, x_height, x_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
x, [x_height, x_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=fold_output_tensor,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -734,6 +742,8 @@ def run(self, input_tensor, device, ops_parallel_config, conv_op_cache={}) -> tt
input_width=self.conv1_input_width,
conv_config=self.conv1_config,
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)
# Relu is fused with conv1
if self.batch_size == 20:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def run_downsample_if_req(
height_sharding=None,
):
if self.downsample:
ds_out, _, _, self.ds_conv_weight_tensor, self.ds_conv_bias_tensor = ttnn.conv2d(
ds_out, [self.ds_conv_weight_tensor, self.ds_conv_bias_tensor] = ttnn.conv2d(
input_tensor=x,
weight_tensor=self.ds_conv_weight_tensor,
in_channels=self.ds_conv_input_channels,
Expand All @@ -187,6 +187,8 @@ def run_downsample_if_req(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_output_dim=False,
return_weights_and_bias=True,
)
ttnn.deallocate(x)
ds_out = ttnn.reallocate(ds_out)
Expand All @@ -209,7 +211,7 @@ def __call__(
# conv1 is 1x1 conv
# print("Running conv1")
module_input_height = input_height
out, input_height, input_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
out, [input_height, input_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=x,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -233,6 +235,8 @@ def __call__(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)

act_block_h_override = 0
Expand Down Expand Up @@ -270,7 +274,7 @@ def __call__(
# self.conv1_input_channels == 256 and
# self.downsample
)
out, input_height, input_width, self.conv2_weight_tensor, self.conv2_bias_tensor = ttnn.conv2d(
out, [input_height, input_width], [self.conv2_weight_tensor, self.conv2_bias_tensor] = ttnn.conv2d(
input_tensor=out,
weight_tensor=self.conv2_weight_tensor,
in_channels=self.conv2_input_channels,
Expand All @@ -297,11 +301,13 @@ def __call__(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)

# conv3 is 1x1 conv
# print("Running conv3")
out, _, _, self.conv3_weight_tensor, self.conv3_bias_tensor = ttnn.conv2d(
out, [self.conv3_weight_tensor, self.conv3_bias_tensor] = ttnn.conv2d(
input_tensor=out,
weight_tensor=self.conv3_weight_tensor,
in_channels=self.conv3_input_channels,
Expand All @@ -324,6 +330,8 @@ def __call__(
reshard_if_not_optimal=reshard_if_not_optimal,
),
conv_op_cache=conv_op_cache,
return_output_dim=False,
return_weights_and_bias=True,
)

if not self.run_downsample_before_conv2:
Expand Down Expand Up @@ -516,7 +524,7 @@ def first_run(self, input_tensor, device, batch_size, ops_parallel_config) -> tt
elif batch_size == 20:
act_block_h_override = 640

x, x_height, x_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
x, [x_height, x_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=input_tensor,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -539,6 +547,8 @@ def first_run(self, input_tensor, device, batch_size, ops_parallel_config) -> tt
act_block_h_override=act_block_h_override,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)
# Relu is fused with conv1

Expand Down Expand Up @@ -819,7 +829,7 @@ def optimized_run(self, input_tensor, device, batch_size, ops_parallel_config, c
else:
act_block_h_override = 0

x, x_height, x_width, self.conv1_weight_tensor, self.conv1_bias_tensor = ttnn.conv2d(
x, [x_height, x_width], [self.conv1_weight_tensor, self.conv1_bias_tensor] = ttnn.conv2d(
input_tensor=input_tensor,
weight_tensor=self.conv1_weight_tensor,
in_channels=self.conv1_input_channels,
Expand All @@ -842,6 +852,8 @@ def optimized_run(self, input_tensor, device, batch_size, ops_parallel_config, c
act_block_h_override=act_block_h_override,
),
conv_op_cache=conv_op_cache,
return_output_dim=True,
return_weights_and_bias=True,
)
# Relu is fused with conv1

Expand Down
Loading

0 comments on commit 009aecf

Please sign in to comment.