jzonthemtn commented on code in PR #1152:
URL: https://github.com/apache/opennlp/pull/1152#discussion_r3552711338


##########
opennlp-extensions/opennlp-embeddings/src/main/java/opennlp/embeddings/SafetensorsFile.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package opennlp.embeddings;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import opennlp.tools.commons.ThreadSafe;
+
+/**
+ * Reads a <a 
href="https://github.com/huggingface/safetensors";>safetensors</a> file: an 
8-byte
+ * little-endian header length, a JSON header describing each tensor's dtype, 
shape, and byte
+ * range, followed by the raw tensor bytes. Deliberately not a general 
tensor-format library:
+ * only the {@code F32} decode path {@link #readFloat32(String)} needs is 
implemented, since
+ * that is what a distilled static-embedding table stores.
+ *
+ * <p><b>Security.</b> Unlike PyTorch's pickle-based checkpoint format, 
safetensors carries no
+ * executable content: the header is data-only JSON and the body is raw tensor 
bytes, so loading
+ * one cannot execute arbitrary code. No hardening beyond ordinary 
malformed-input handling is
+ * needed.</p>
+ *
+ * <p>The whole file is read into memory up front (matching the project's 
existing bundled-data
+ * readers), which is appropriate for the small (tens of megabytes) tables 
this module targets.
+ * Instances are immutable and safe for concurrent reads after construction; 
every
+ * {@link #readFloat32(String)} call decodes into a fresh array the caller 
owns.</p>
+ */
+@ThreadSafe
+public final class SafetensorsFile {
+
+  private static final int HEADER_LENGTH_PREFIX_BYTES = 8;
+
+  private final byte[] bytes;
+  private final long dataStart;
+  private final Map<String, TensorInfo> tensorsByName;
+  private final Map<String, String> metadata;
+
+  private SafetensorsFile(byte[] bytes, long dataStart, Map<String, 
TensorInfo> tensorsByName,
+                           Map<String, String> metadata) {
+    this.bytes = bytes;
+    this.dataStart = dataStart;
+    this.tensorsByName = tensorsByName;
+    this.metadata = metadata;
+  }
+
+  /**
+   * Reads a safetensors file.
+   *
+   * @param file The file to read. Must not be {@code null} and must exist.
+   * @return The parsed file, with every tensor's metadata resolved and 
validated against the
+   *     file's actual length.
+   * @throws IllegalArgumentException Thrown if {@code file} is {@code null} 
or missing, or the
+   *     file is malformed.
+   * @throws UncheckedIOException Thrown if reading the file fails.
+   */
+  public static SafetensorsFile read(Path file) {
+    if (file == null) {
+      throw new IllegalArgumentException("File must not be null");
+    }
+    if (!Files.isRegularFile(file)) {
+      throw new IllegalArgumentException("File does not exist or is not a 
regular file: " + file);
+    }
+    final byte[] bytes;
+    try {
+      bytes = Files.readAllBytes(file);

Review Comment:
   I think a `byte[]`'s max size is around 2 GB...? Any concerns that the file 
could be larger than that?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to