@qvac/dl-hyperdrive
@qvac/dl-hyperdrive is a data loading library designed to load model weights and other resources from a Hyperdrive instance. It leverages the Hyperdrive distributed file system for efficient peer-to-peer file sharing, enabling dynamic loading of files required for AI model inference, training, and other operations.
Usage
HyperDriveDL Class
HyperDriveDL extends BaseDL to provide a unified interface for loading files from a Hyperdrive instance. It integrates seamlessly with other QVAC AI Runtime libraries and classes.
Constructor:
const HyperDriveDL = require("@qvac/dl-hyperdrive");
const hdDL = new HyperDriveDL({ key: "hd://your_hyperdrive_key" });
key (optional): A string representing the Hyperdrive key with the hd:// protocol prefix.
store (optional): A Corestore instance for managing storage. If not provided, the Hyperdrive will use an in-memory store.
drive (optional): A Hyperdrive instance. If provided, the Hyperdrive will use the provided instance instead of creating a new one.
Note: If both key and drive are provided, the key will be ignored.
Methods:
-
ready(): Initializes the Hyperdrive client. This method must be called before interacting with the Hyperdrive.
await hdDL.ready();
-
getStream(path): Asynchronously retrieves a readable stream for a specified file path in the Hyperdrive.
const stream = await hdDL.getStream("/path/to/file");
-
list(directoryPath): Asynchronously lists files in a given directory in the Hyperdrive. By default, it lists files from the root directory.
const files = await hdDL.list("/");
console.log(files);
-
download(path, opts): Downloads files to local cache or directly to disk. Returns an object with trackers that can be used to monitor and cancel downloads.
const download = await hdDL.download("/path/to/file");
console.log("Download started", download);
const results = await download.await();
console.log("Download completed:", results);
const dirDownload = await hdDL.download("/path/to/directory/");
console.log(`Directory download started with ${dirDownload.trackers.length} trackers`);
const progressReport = new ProgressReport({ "/path/to/file": 1000 }, callback);
const downloadWithProgress = await hdDL.download("/path/to/file", progressReport);
const progressResults = await downloadWithProgress.await();
const diskDownload = await hdDL.download("/path/to/file", {
diskPath: "./downloads"
});
const diskResults = await diskDownload.await();
const diskProgressDownload = await hdDL.download("/path/to/file", {
diskPath: "./downloads",
progressReporter: progressReport
});
const callbackDownload = await hdDL.download("/path/to/file", {
diskPath: "./downloads",
progressCallback: (data) => console.log("Progress:", data)
});
-
await(): Waits for all downloads to complete and returns an array of results. This method is available on the download object returned by download().
-
cancel(): Cancels active downloads. This method is available on the download object returned by download().
const download = await hdDL.download("/path/to/file");
const results = await download.await();
console.log(results);
results.forEach(result => {
if (result.error) {
console.error(`Failed to download ${result.file}:`, result.error);
} else {
console.log(`Successfully downloaded ${result.file}`);
}
});
await download.cancel();
const file1Download = await hdDL.download("/file1.txt");
const file2Download = await hdDL.download("/file2.txt");
const results1 = await file1Download.await();
const results2 = await file2Download.await();
await file1Download.cancel();
await file2Download.cancel();
-
cached(path): Checks if a file or directory is cached locally.
const isCached = await hdDL.cached("/path/to/file");
console.log(`File is cached: ${isCached}`);
-
deleteLocal(path): Deletes cached files from local storage.
const deleted = await hdDL.deleteLocal("/path/to/file");
console.log(`File deleted: ${deleted}`);
await hdDL.deleteLocal();
-
close(): Stops the Hyperdrive client and releases resources.
await hdDL.close();
Examples
Loading Models with QVAC Runtime
Below is an example of how HyperDriveDL can be used within the QVAC AI Runtime to dynamically load models:
const Qvac = require("qvac-rt-local");
const HyperDriveDL = require("@qvac/dl-hyperdrive");
const MLCWhisper = require("qvac-lib-inference-addon-mlc-whisper");
const qvac = new Qvac({
});
const whisper = qvac.inference.add(
new MLCWhisper({
weights: new HyperDriveDL({ key: "hd://your_hyperdrive_key" }),
params: {
},
})
);
await whisper.load();
HyperDriveDL in AI Models
The HyperDriveDL class can be integrated directly within model classes to dynamically fetch and load model files from a Hyperdrive instance.
class MyModel {
constructor(loader) {
this.loader = loader;
}
async load() {
const weightsStream = await this.loader.getStream("model_weights.bin");
}
}
Script Usage
This repository includes a script that allows you to serve files from a specified folder over Hyperdrive. To run the script using npm:
-
Run the script with a folder path argument:
npm run hd-provider -- ./path/to/model/files ./path/to/storage(optional)
This command will start serving files from the specified folder over Hyperdrive and output the Hyperdrive key, which can be used by clients to access the files.
The path to storage is optional. If it's not provided, the files will be served from memory.
Development
-
Install dependencies:
Before proceeding with the installation, please generate a granular Personal Access Token (PAT) with the read-only scope. Once generated, add the token to your environment variables using the name NPM_TOKEN.
export NPM_TOKEN=your_personal_access_token
Next, create a .npmrc file in the root of your project with the following content:
@qvac:registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken={NPM_TOKEN}
This configuration ensures secure access to NPM Packages when installing scoped packages.
npm install
-
Run unit tests:
npm test
-
Run all tests with coverage and generate HTML reports:
npm run coverage:unit # Unit test coverage (HTML: coverage/unit/index.html)
npm run coverage:integration # Integration test coverage (HTML: coverage/integration/index.html)
npm run coverage # All tests coverage (HTML: coverage/all/index.html)
- These commands run the respective test suites and generate coverage reports in the
coverage/unit, coverage/integration, and coverage/all directories.
- To view the HTML report, open the corresponding
index.html file in your browser.
-
Run only unit or integration tests (without coverage):
npm run test:unit
npm run test:integration
- All test files must be named with the
.test.js suffix and placed in the appropriate test/unit or test/integration directories.
- Coverage is collected using
brittle-bare --coverage and HTML reports are generated using Istanbul.