Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic 2D layer_norm operator #588

Merged
merged 1 commit into from
Feb 19, 2024
Merged

Add basic 2D layer_norm operator #588

merged 1 commit into from
Feb 19, 2024

Conversation

xenova
Copy link
Collaborator

@xenova xenova commented Feb 15, 2024

Needed for nomic-ai/nomic-embed-text-v1.5.

Example usage:

import { pipeline, layer_norm } from '@xenova/transformers';

// Create a feature extraction pipeline
const extractor = await pipeline('feature-extraction', 'nomic-ai/nomic-embed-text-v1.5', {
    quantized: false, // Comment out this line to use the quantized version
});

// Compute sentence embeddings
const texts = ['search_query: What is TSNE?', 'search_query: Who is Laurens van der Maaten?'];
let embeddings = await extractor(texts, { pooling: 'mean' });

const matryoshka_dim = 512;
embeddings = layer_norm(embeddings, [embeddings.dims[1]])
    .slice(null, [0, matryoshka_dim])
    .normalize(2, -1);
console.log(embeddings.tolist());
// [
//   [-0.00518727907910943,   0.06514579057693481,  -0.21559129655361176, ...],
//   [-0.008253306150436401,  0.005108598619699478,   -0.22179779410362244, ...],
// ]

Relevant discussion: https://huggingface.co/nomic-ai/nomic-embed-text-v1.5/discussions/4#65cce8d0c52afc14ceac26c2

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova xenova merged commit e9092d2 into main Feb 19, 2024
4 checks passed
@xenova xenova deleted the add-layer-norm branch February 19, 2024 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants