Skip to content

Commit

Permalink
Update tests, vite handling packaging, e2e, unit
Browse files Browse the repository at this point in the history
  • Loading branch information
rockerBOO committed Nov 7, 2024
1 parent 7164927 commit f20d9a3
Show file tree
Hide file tree
Showing 22 changed files with 800 additions and 12,186 deletions.
50 changes: 42 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
# Makefile for building and running WASM projects

# General settings
WASM_DIR := crates/lora-inspector-wasm
OUT_DIR := pkg
# TARGET := --target bundler
TARGET := --target web
# RELEASE := --release
RELEASE :=
# WEAK_REFS := --weak-refs
WEAK_REFS :=

# Default target
.PHONY: all
all: test build-wasm

# Run tests for the whole workspace
.PHONY: test
test:
cargo test --workspace
cargo test --workspace && \
make wasm-bindgen-test && \
yarn --cwd crates/lora-inspector-wasm test && \
yarn --cwd crates/lora-inspector-wasm e2e-test

# Build WASM for production (optimized)
.PHONY: build-wasm
build-wasm:
wasm-pack build --target no-modules --out-dir pkg crates/lora-inspector-wasm --release --weak-refs

build-dev-wasm:
wasm-pack build --target no-modules --out-dir pkg crates/lora-inspector-wasm --release --weak-refs
wasm-pack build $(TARGET) --out-dir $(OUT_DIR) $(WASM_DIR) $(RELEASE) $(WEAK_REFS) && \
yarn --cwd $(WASM_DIR) build

# Start a local HTTP server for serving the WASM package (simple)
.PHONY: dev-wasm
dev-wasm:
python -m http.server -d crates/lora-inspector-wasm
make build-wasm && \
cd $(WASM_DIR) && \
yarn vite

# Start a custom server (e.g., with CORS enabled) for development
.PHONY: dev-wasm-cors
dev-wasm-cors:
cd $(WASM_DIR) && python simple-cors-server.py

.PHONY:
wasm-bindgen-test:
wasm-pack test --headless --firefox crates/lora-inspector-wasm

dev-wasm-2:
cd crates/lora-inspector-wasm/ && python simple-cors-server.py
# Clean build artifacts (optional)
.PHONY: clean
clean:
rm -rf $(OUT_DIR)/*
41 changes: 30 additions & 11 deletions crates/inspector/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,23 @@ impl Weight for BufferedLoRAWeight {
/ dims[0] as f64;

if dims.len() == 2 {
up.matmul(&down)?.detach()?.mul(scale)?.detach()
to_compatible_dtype(&up)?
.matmul(&to_compatible_dtype(&down)?)?
.detach()?
.mul(scale)?
.detach()
} else if dims[2] == 1 && dims[3] == 1 {
up.squeeze(3)?
to_compatible_dtype(&up)?
.squeeze(3)?
.squeeze(2)?
.matmul(&down.squeeze(3)?.squeeze(2)?)?
.matmul(&to_compatible_dtype(&down)?.squeeze(3)?.squeeze(2)?)?
.unsqueeze(2)?
.unsqueeze(3)?
.mul(scale)
} else {
down.permute((1, 0, 2, 3))?
.conv2d(&up, 0, 1, 1, 1)?
to_compatible_dtype(&down)?
.permute((1, 0, 2, 3))?
.conv2d(&to_compatible_dtype(&up)?, 0, 1, 1, 1)?
.permute((1, 0, 2, 3))?
.mul(scale)
}
Expand Down Expand Up @@ -558,6 +564,15 @@ pub struct LoRAWeight {
pub device: Device,
}

/// Converts tensor into DType thats compatible with candle
/// We convert BF16 to F32
fn to_compatible_dtype(tensor: &candle_core::Tensor) -> Result<Tensor, candle_core::Error> {
match tensor.dtype() {
candle_core::DType::BF16 => tensor.to_dtype(candle_core::DType::F32),
_ => Ok(tensor.clone()),
}
}

impl LoRAWeight {
pub fn new(buffer: Vec<u8>, device: &Device) -> Result<Self, candle_core::Error> {
Ok(Self {
Expand Down Expand Up @@ -660,17 +675,21 @@ impl Weight for LoRAWeight {
let scale = alpha.to_scalar::<f64>()? / dims[0] as f64;

if dims.len() == 2 {
up.matmul(down)?.mul(scale)
to_compatible_dtype(up)?
.matmul(&to_compatible_dtype(down)?)?
.mul(scale)
} else if dims[2] == 1 && dims[3] == 1 {
up.squeeze(3)?
to_compatible_dtype(up)?
.squeeze(3)?
.squeeze(2)?
.matmul(&down.squeeze(3)?.squeeze(2)?)?
.matmul(&to_compatible_dtype(down)?.squeeze(3)?.squeeze(2)?)?
.unsqueeze(2)?
.unsqueeze(3)?
.mul(scale)
} else {
down.permute((1, 0, 2, 3))?
.conv2d(up, 0, 1, 1, 1)?
to_compatible_dtype(down)?
.permute((1, 0, 2, 3))?
.conv2d(&to_compatible_dtype(up)?, 0, 1, 1, 1)?
.permute((1, 0, 2, 3))?
.mul(scale)
}
Expand Down Expand Up @@ -732,7 +751,7 @@ impl Weight for LoRAWeight {
let t1_w1 = t1.mul(&one)?;
let t2_w2 = t2.mul(&two)?;

t1_w1.matmul(&t2_w2)?.mul(scale)
t1_w1.matmul(&t2_w2)? * scale
} else {
let one = w1_a.matmul(w1_b)?;
let two = w2_a.matmul(w2_b)?;
Expand Down
8 changes: 8 additions & 0 deletions crates/lora-inspector-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

node_modules/
/test-results/
/playwright-report/
Expand Down
1 change: 1 addition & 0 deletions crates/lora-inspector-wasm/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
21 changes: 21 additions & 0 deletions crates/lora-inspector-wasm/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Dave Lage (rockerBOO)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions crates/lora-inspector-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Web application for LoRA inspector

## Install

```
yarn install --immutable
```

## Usage

### Dev

```
yarn dev
```

### Build

```
yarn build
```

### Test

```
yarn test
```

This file was deleted.

Loading

0 comments on commit f20d9a3

Please sign in to comment.