Skip to content

Commit 9630f9d

Browse files
jorgep31415facebook-github-bot
authored andcommitted
Introduce add_tensor_like consuming ValueRef (#2835)
Summary: Pull Request resolved: #2835 From SS-JIA: > we should always make sure to store references produced from `graph.get_val()` only after any calls to `graph.add_*()` (i.e. modifications to the values list) are made. This is because `graph.values_`, being a `std::vector`, will reallocate with more space and move its contents if the current allocation is not sufficient. This means that if you store a reference then call `graph.add_*()` then the underlying resource the reference points to may have been moved. We can guard against this behavior by passing a `ValueRef` directly, and never having to declare a variable of types `ValueRef&/TensorRef&` in the caller's scope. An example is shown in `Staging.cpp`. ghstack-source-id: 221721758 exported-using-ghexport bypass-github-export-checks Reviewed By: SS-JIA Differential Revision: D55703483 fbshipit-source-id: 330976df2ba8467dd028d79741eceb17ae8c2d43
1 parent 7f1fa70 commit 9630f9d

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

backends/vulkan/runtime/graph/ComputeGraph.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,27 @@ ValueRef ComputeGraph::add_tensor(
132132
sizes, dtype, suggested_storage_type(), memory_layout, shared_object_idx);
133133
}
134134

135+
ValueRef ComputeGraph::add_tensor_like(
136+
const ValueRef vref,
137+
const api::StorageType storage_type,
138+
const api::GPUMemoryLayout memory_layout) {
139+
TensorRef& tref = get_val(vref).toTensorRef();
140+
return add_tensor(tref.sizes, tref.dtype, storage_type, memory_layout);
141+
}
142+
143+
ValueRef ComputeGraph::add_tensor_like(
144+
const ValueRef vref,
145+
const api::GPUMemoryLayout memory_layout) {
146+
TensorRef& tref = get_val(vref).toTensorRef();
147+
return add_tensor(tref.sizes, tref.dtype, memory_layout);
148+
}
149+
135150
ValueRef ComputeGraph::add_tensor(
136151
const std::vector<int64_t>& sizes,
137152
const api::ScalarType dtype,
138153
const int64_t shared_object_idx) {
139154
return add_tensor(
140-
sizes,
141-
dtype,
142-
suggested_storage_type(),
143-
suggested_memory_layout(sizes),
144-
shared_object_idx);
155+
sizes, dtype, suggested_memory_layout(sizes), shared_object_idx);
145156
}
146157

147158
ValueRef ComputeGraph::add_tensorref(

backends/vulkan/runtime/graph/ComputeGraph.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class ComputeGraph final {
172172
const api::ScalarType dtype,
173173
const api::StorageType storage_type,
174174
const api::GPUMemoryLayout memory_layout,
175-
const int64_t shared_object_idx);
175+
const int64_t shared_object_idx = -1);
176176

177177
/*
178178
* Add a `vTensor` value to the graph with the specified properties. The
@@ -191,9 +191,25 @@ class ComputeGraph final {
191191
*/
192192
ValueRef add_tensor(
193193
const std::vector<int64_t>& sizes,
194-
const api::ScalarType dtype = api::ScalarType::Float,
194+
const api::ScalarType dtype,
195195
const int64_t shared_object_idx = -1);
196196

197+
/*
198+
* Add a `vTensor` value to the graph with the properties of `vref`.
199+
*/
200+
ValueRef add_tensor_like(
201+
const ValueRef vref,
202+
const api::StorageType storage_type,
203+
const api::GPUMemoryLayout memory_layout);
204+
205+
/*
206+
* Add a `vTensor` value to the graph with the properties of `vref`. The
207+
* suggested storage type will be used to construct the `vTensor`.
208+
*/
209+
ValueRef add_tensor_like(
210+
const ValueRef vref,
211+
const api::GPUMemoryLayout memory_layout);
212+
197213
/*
198214
* Add a `TensorRef` value to the graph with the specific properties. A
199215
* `TensorRef` is a reference to a `vTensor` whose data is stored in an

backends/vulkan/runtime/graph/ops/impl/Staging.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ ValueRef prepack(
6363
ComputeGraph& graph,
6464
const ValueRef vref,
6565
const api::GPUMemoryLayout layout) {
66-
TensorRef& tref = graph.get_val(vref).toTensorRef();
67-
ValueRef v = graph.add_tensor(tref.sizes, tref.dtype, layout);
66+
ValueRef v = graph.add_tensor_like(vref, layout);
6867
vTensor& t = graph.get_val(v).toTensor();
6968

7069
api::ShaderInfo shader = get_nchw_to_image_shader(t);

0 commit comments

Comments
 (0)