Skip to content

Commit

Permalink
Fix structural pruning sparsity notebook
Browse files Browse the repository at this point in the history
- Enabled tensor preservation option explicitly when creating TFLite
  interpreter.
- The convolution weight search is now done via the operator lookup.

PiperOrigin-RevId: 595845109
  • Loading branch information
abattery authored and tensorflower-gardener committed Jan 5, 2024
1 parent a57f59d commit e38d886
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"id": "FbORZA_bQx1G"
},
"source": [
"Structural pruning weights from your model to make it sparse in specific pattern can accelerate model inference time with appropriate HW supports. \n",
"Structural pruning weights from your model to make it sparse in specific pattern can accelerate model inference time with appropriate HW supports.\n",
"\n",
"This tutorial shows you how to:\n",
"* Define and train a model on the mnist dataset with a specific structural sparsity\n",
Expand Down Expand Up @@ -459,7 +459,7 @@
"outputs": [],
"source": [
"# Load tflite file with the created pruned model\n",
"interpreter = tf.lite.Interpreter(model_path=tflite_file)\n",
"interpreter = tf.lite.Interpreter(model_path=tflite_file, experimental_preserve_all_tensors=True)\n",
"interpreter.allocate_tensors()\n",
"\n",
"details = interpreter.get_tensor_details()\n",
Expand Down Expand Up @@ -630,9 +630,10 @@
"outputs": [],
"source": [
"# Get weights of the convolutional layer that has been pruned with 2 by 4 sparsity.\n",
"tensor_name = 'structural_pruning/Conv2D'\n",
"detail = [x for x in details if tensor_name in x[\"name\"]]\n",
"tensor_data = interpreter.tensor(detail[1][\"index\"])()\n",
"op_details = interpreter._get_ops_details()\n",
"op_name = 'CONV_2D'\n",
"op_detail = [x for x in op_details if op_name in x[\"op_name\"]]\n",
"tensor_data = interpreter.tensor(op_detail[1][\"inputs\"][1])()\n",
"print(f\"Shape of the weight tensor is {tensor_data.shape}\")"
]
},
Expand Down

0 comments on commit e38d886

Please sign in to comment.