Class: Cohere::Transcribe::PyTorchCheckpoint::Reader
- Inherits:
-
Object
- Object
- Cohere::Transcribe::PyTorchCheckpoint::Reader
- Defined in:
- lib/cohere/transcribe/pytorch_checkpoint.rb
Overview
One checkpoint shard. Tensor bytes are never materialized as a whole.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#tensors ⇒ Object
readonly
Returns the value of attribute tensors.
Instance Method Summary collapse
- #fetch(name) ⇒ Object
-
#initialize(path) ⇒ Reader
constructor
A new instance of Reader.
- #key?(name) ⇒ Boolean
- #names ⇒ Object
- #write_tensor(tensor, output, target_dtype:, converter: Safetensors::DTypeConverter.default, chunk_bytes: Safetensors::DEFAULT_CHUNK_BYTES) ⇒ Object
Constructor Details
#initialize(path) ⇒ Reader
Returns a new instance of Reader.
771 772 773 774 775 |
# File 'lib/cohere/transcribe/pytorch_checkpoint.rb', line 771 def initialize(path) @path = Pathname(path). @storages = {} @tensors = read_checkpoint.freeze end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
769 770 771 |
# File 'lib/cohere/transcribe/pytorch_checkpoint.rb', line 769 def path @path end |
#tensors ⇒ Object (readonly)
Returns the value of attribute tensors.
769 770 771 |
# File 'lib/cohere/transcribe/pytorch_checkpoint.rb', line 769 def tensors @tensors end |
Instance Method Details
#fetch(name) ⇒ Object
785 786 787 788 789 |
# File 'lib/cohere/transcribe/pytorch_checkpoint.rb', line 785 def fetch(name) tensors.fetch(name) rescue KeyError raise Error, "Tensor #{name.inspect} is not present in #{path}" end |
#key?(name) ⇒ Boolean
781 782 783 |
# File 'lib/cohere/transcribe/pytorch_checkpoint.rb', line 781 def key?(name) tensors.key?(name) end |
#names ⇒ Object
777 778 779 |
# File 'lib/cohere/transcribe/pytorch_checkpoint.rb', line 777 def names tensors.keys end |
#write_tensor(tensor, output, target_dtype:, converter: Safetensors::DTypeConverter.default, chunk_bytes: Safetensors::DEFAULT_CHUNK_BYTES) ⇒ Object
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 |
# File 'lib/cohere/transcribe/pytorch_checkpoint.rb', line 791 def write_tensor(tensor, output, target_dtype:, converter: Safetensors::DTypeConverter.default, chunk_bytes: Safetensors::DEFAULT_CHUNK_BYTES) raise ArgumentError, "Tensor #{tensor.name.inspect} belongs to a different PyTorch reader" unless tensor.reader.equal?(self) target_dtype = target_dtype.to_s.upcase unless FLOAT_DTYPES.include?(tensor.dtype) && FLOAT_DTYPES.include?(target_dtype) raise Error, "Cannot convert #{tensor.name.inspect} from #{tensor.dtype} to #{target_dtype}" end raise ArgumentError, "chunk_bytes must be positive" unless chunk_bytes.positive? source_width = Safetensors::DTYPE_BYTES.fetch(tensor.dtype) elements_per_chunk = [chunk_bytes / source_width, 1].max bytes_per_chunk = elements_per_chunk * source_width unless contiguous?(tensor) return write_strided_tensor( tensor, output, target_dtype: target_dtype, converter: converter, elements_per_chunk: elements_per_chunk, source_width: source_width ) end remaining = tensor.nbytes written = 0 File.open(path, "rb") do |source| source.seek(tensor.data_start) while remaining.positive? requested = [remaining, bytes_per_chunk].min chunk = source.read(requested) raise Error, "Unexpected end of #{path} while reading #{tensor.name.inspect}" if chunk.nil? || chunk.bytesize != requested converted = converter.convert(chunk, from: tensor.dtype, to: target_dtype) output.write(converted) written += converted.bytesize remaining -= requested end end expected = tensor.element_count * Safetensors::DTYPE_BYTES.fetch(target_dtype) return written if written == expected raise Error, "Converted tensor #{tensor.name.inspect} wrote #{written} bytes; expected #{expected}" end |