Skip to content

Commit

Permalink
Added prefix and recurse options to named_buffers method [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Aug 6, 2024
1 parent 7baafd0 commit 18f2122
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.17.1 (unreleased)

- Added `persistent` option to `register_buffers`
- Added `prefix` and `recurse` options to `named_buffers` method

## 0.17.0 (2024-07-26)

Expand Down
14 changes: 12 additions & 2 deletions lib/torch/nn/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,18 @@ def buffers
named_buffers.values
end

def named_buffers
@buffers || {}
# TODO set recurse: true in 0.18.0
def named_buffers(prefix: "", recurse: false)
buffers = {}
if recurse
named_children.each do |name, mod|
buffers.merge!(mod.named_buffers(prefix: "#{prefix}#{name}.", recurse: recurse))
end
end
(@buffers || {}).each do |k, v|
buffers[[prefix, k].join] = v
end
buffers
end

def children
Expand Down

0 comments on commit 18f2122

Please sign in to comment.