Skip to content

Commit e07d15e

Browse files
Olivia-liufacebook-github-bot
authored andcommitted
Remove 0 bytes lines; merge getitem with batch_norm nodes
Differential Revision: D65121374
1 parent 41a57e6 commit e07d15e

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

exir/memory_planning.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ def collect_specs_from_nodes( # noqa: C901
421421
if spec in unique_spec:
422422
continue
423423
else:
424+
spec.node_name = node.name
424425
unique_spec.add(spec)
425426
yield spec
426427

@@ -501,6 +502,7 @@ def pick_shared_obj(
501502
"""
502503
# TODO: do better than linear scan
503504
picked = None
505+
# This function goes over all of tensors and figure out which of them can share memory with no conflict.
504506
for sobj in shared_objects:
505507
if spec.lifetime[0] > sobj.last_used_index:
506508
if picked is None or _size_abs_dif(sobj, spec) < _size_abs_dif(

exir/tensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def __init__(
146146
self.is_sparse = is_sparse
147147
self.init_mem_planning_fields()
148148
self.shape_dynamism: TensorShapeDynamism = determine_tensor_dynanism(self.shape)
149+
self.node_name = None
149150

150151
@property
151152
def allocated_memory(self) -> int:

util/activation_memory_profiler.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Allocation:
2828
size_bytes: int
2929
fqn: str
3030
file_and_line_num: str
31+
overlap_with_subsequent: bool = False
3132

3233

3334
@dataclass
@@ -76,17 +77,18 @@ def create_tensor_allocation_info(graph: torch.fx.Graph) -> List[MemoryTimeline]
7677
# pyre-ignore
7778
memory_timeline[j] = MemoryTimeline()
7879
# pyre-ignore
79-
memory_timeline[j].allocations.append(
80-
Allocation(
81-
node.name,
82-
node.target,
83-
tensor_spec.mem_id,
84-
tensor_spec.mem_offset,
85-
size,
86-
fqn,
87-
stack_trace,
88-
)
89-
)
80+
if size != 0:
81+
memory_timeline[j].allocations.append(
82+
Allocation(
83+
node.name,
84+
node.target,
85+
tensor_spec.mem_id,
86+
tensor_spec.mem_offset,
87+
size,
88+
fqn,
89+
stack_trace,
90+
))
91+
9092
# pyre-ignore
9193
return memory_timeline
9294

@@ -137,6 +139,15 @@ def generate_memory_trace(
137139
start_time = 0
138140
if memory_timeline_event is None:
139141
continue
142+
# "collapse" tensors that shared the same memory space at a given time
143+
for index, element in enumerate(memory_timeline_event.allocations):
144+
if index == len(memory_timeline_event.allocations) - 1:
145+
break
146+
if (
147+
element.memory_offset
148+
== memory_timeline_event.allocations[index + 1].memory_offset
149+
):
150+
element.overlap_with_subsequent = True
140151
for allocation in memory_timeline_event.allocations:
141152
e = {}
142153
e["name"] = allocation.name
@@ -159,7 +170,8 @@ def generate_memory_trace(
159170
e["args"]["fqn"] = f"{allocation.fqn}"
160171
e["args"]["source"] = f"{allocation.file_and_line_num}"
161172
e["args"]["bytes"] = allocation.size_bytes
162-
start_time += allocation_size_kb
173+
if not allocation.overlap_with_subsequent:
174+
start_time += allocation_size_kb
163175
trace_events.append(e)
164176
tid += 1
165177

0 commit comments

Comments
 (0)