Skip to content

Commit

Permalink
Run spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
tjake committed Aug 26, 2024
1 parent 28bf13f commit 36d9640
Show file tree
Hide file tree
Showing 49 changed files with 613 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
*/
package com.github.tjake.jlama.cli.commands;

import static com.github.tjake.jlama.model.ModelSupport.loadModel;

import com.github.tjake.jlama.model.AbstractModel;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import picocli.CommandLine;

import java.util.Optional;

import static com.github.tjake.jlama.model.ModelSupport.loadModel;

@CommandLine.Command(name = "restapi", description = "Starts a openai compatible rest api for interacting with this model")
@CommandLine.Command(
name = "restapi",
description = "Starts a openai compatible rest api for interacting with this model")
@SpringBootApplication(scanBasePackages = {"com.github.tjake.jlama.net.openai", "com.github.tjake.jlama.cli.commands"})
@SpringBootConfiguration
@Configuration
Expand All @@ -54,8 +54,7 @@ public AbstractModel getModelBean() {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/ui/**")
.addResourceLocations("classpath:/static/ui/");
registry.addResourceHandler("/ui/**").addResourceLocations("classpath:/static/ui/");
}

@Override
Expand All @@ -74,9 +73,7 @@ public void run() {

new SpringApplicationBuilder(ApiServiceCommand.class)
.lazyInitialization(true)
.properties(
"server.port", ""+port,
"logging.level.org.springframework.web", "debug")
.properties("server.port", "" + port, "logging.level.org.springframework.web", "debug")
.build()
.run();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.github.tjake.jlama.cli.commands;



import com.github.tjake.jlama.net.Coordinator;
import picocli.CommandLine;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class VectorMath {
public static void pfor(int start, int end, IntConsumer action) {
PhysicalCoreExecutor.instance
.get()
.execute(() -> IntStream.range(start, end)
.parallel()
.forEach(action));
.execute(() -> IntStream.range(start, end).parallel().forEach(action));
}

public static void pchunk(int offset, int length, BiIntConsumer action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.github.tjake.jlama.model;

import static com.github.tjake.jlama.util.DebugSupport.debug;

import com.github.tjake.jlama.math.VectorMath;
import com.github.tjake.jlama.model.functions.EmbedInput;
import com.github.tjake.jlama.model.functions.Generator;
Expand Down Expand Up @@ -45,8 +47,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.github.tjake.jlama.util.DebugSupport.debug;

public abstract class AbstractModel implements Generator {
private static final Logger logger = LoggerFactory.getLogger(AbstractModel.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
package com.github.tjake.jlama.model;

import static com.github.tjake.jlama.util.DebugSupport.debug;

import com.github.tjake.jlama.math.VectorMath;
import com.github.tjake.jlama.safetensors.Config;
import com.github.tjake.jlama.safetensors.DType;
import com.github.tjake.jlama.tensor.AbstractTensor;
import com.github.tjake.jlama.tensor.BFloat16BufferTensor;
import com.github.tjake.jlama.tensor.FloatBufferTensor;
import com.github.tjake.jlama.tensor.operations.TensorOperations;
import com.github.tjake.jlama.tensor.operations.TensorOperationsProvider;
import com.google.common.base.Preconditions;
import java.util.*;
import java.util.function.Consumer;

import static com.github.tjake.jlama.util.DebugSupport.debug;

public class CausalSelfAttention {
private final AbstractModel m;
private final Config c;
Expand Down Expand Up @@ -257,8 +253,7 @@ public AbstractTensor forward(
int offset = h * c.headSize;

// skip if we are out of bounds
if (offset >= query.shape().last())
break;
if (offset >= query.shape().last()) break;

int goffset = c.maybeMapToGroupHead(h) * c.headSize;
// rotate q by the freq theta and freq r
Expand All @@ -276,8 +271,7 @@ public AbstractTensor forward(
for (int h = c.groupHeadStart(); h < c.groupHeadEnd(); h++) {
// get the k vectors for this head
int offset = h * c.headSize;
if (offset >= key.shape().last())
break;
if (offset >= key.shape().last()) break;
// rotate k by the freq theta and freq r
for (int i = offset; i < (offset + headPiece); i++) {
float k00 = key.get(0, i);
Expand Down Expand Up @@ -314,15 +308,13 @@ public AbstractTensor forward(
debug("key+rope", key, finalPostion);
});


// Attention
VectorMath.pfor(c.headStart(), c.headEnd(), h -> {
try (AbstractTensor attn = m.makeFullTensor(1, kvp.shape().first())) {
int xoffset = c.maybeMapToGroupHead(h) * c.headSize;
int yoffset = h * c.headSize;

if (yoffset >= query.shape().last())
return;
if (yoffset >= query.shape().last()) return;

// compute attention scores by multiplying query and key for every position
TensorOperationsProvider.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
*/
package com.github.tjake.jlama.model;

import static com.github.tjake.jlama.util.DebugSupport.debug;

import com.github.tjake.jlama.model.functions.FeedForward;
import com.github.tjake.jlama.tensor.AbstractTensor;
import com.github.tjake.jlama.tensor.operations.TensorOperationsProvider;
import com.github.tjake.jlama.util.DebugSupport;
import com.github.tjake.jlama.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import static com.github.tjake.jlama.util.DebugSupport.debug;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TransformerBlock {

Expand Down Expand Up @@ -94,7 +92,6 @@ public AbstractTensor forward(
AbstractTensor lnemb =
preAttentionNorm.map(ln -> ln.forward(embedding, normReducer)).orElse(embedding);


debug("ln_emb", lnemb, layerIndex);

AbstractTensor postAttention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.tjake.jlama.math.ActivationFunction;
import com.github.tjake.jlama.safetensors.Config;

import java.util.List;

public class BertConfig extends Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.tjake.jlama.math.ActivationFunction;
import com.github.tjake.jlama.safetensors.Config;

import java.util.List;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ protected TransformerBlock[] loadTransformerBlockWeights() {
weights.load(prefix + "up_proj.weight", c.offset()).quantize(qType)); // w3

transformerBlocks[i] = new TransformerBlock(
this, i,
this,
i,
new RMSNorm(
this,
weights.load(base + "input_layernorm.weight", c.offset())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.tjake.jlama.math.ActivationFunction;
import com.github.tjake.jlama.safetensors.Config;

import java.util.List;

public class GPT2Config extends Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import com.github.tjake.jlama.safetensors.tokenizer.BPETokenizer;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import net.fellbaum.jemoji.EmojiManager;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import net.fellbaum.jemoji.EmojiManager;

public class GPT2Tokenizer extends BPETokenizer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.tjake.jlama.math.ActivationFunction;
import com.github.tjake.jlama.safetensors.Config;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -53,6 +52,8 @@ public LlamaConfig(
eosToken instanceof List<?> ? (List<Integer>) eosToken : List.of((Integer) eosToken),
activationFunction,
ropeFreqsTheta == null ? 10000.0 : ropeFreqsTheta,
ropeScaling == null || !("linear".equals(ropeScaling.get("rope_type"))) ? 1.0 : Double.parseDouble(ropeScaling.get("factor")));
ropeScaling == null || !("linear".equals(ropeScaling.get("rope_type")))
? 1.0
: Double.parseDouble(ropeScaling.get("factor")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ protected EmbedInput loadInputWeights() {
at = TensorOperationsProvider.get()
.quantize(at, embedding.dType(), c.embeddingSegmentStart(), c.embeddingSegmentLength());


embedding.copyFrom(
at,
at.getOffset(0, c.embeddingSegmentStart()),
Expand Down Expand Up @@ -110,7 +109,8 @@ protected TransformerBlock[] loadTransformerBlockWeights() {
weights.load(prefix + "up_proj.weight", c.offset()).quantize(qType)); // w3

transformerBlocks[i] = new TransformerBlock(
this, i,
this,
i,
new RMSNorm(
this,
weights.load(base + "input_layernorm.weight", c.offset())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class LlamaTokenizer extends BPETokenizer {

public LlamaTokenizer(Path modelRoot) {
super(modelRoot);
this.byteFallbackEncodingOffset = this.getModel().vocabLookup.getOrDefault("<0x00>", 0L).intValue();
this.byteFallbackEncodingOffset =
this.getModel().vocabLookup.getOrDefault("<0x00>", 0L).intValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.tjake.jlama.math.ActivationFunction;
import com.github.tjake.jlama.safetensors.Config;

import java.util.List;

public class MistralConfig extends Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.tjake.jlama.math.ActivationFunction;
import com.github.tjake.jlama.safetensors.Config;

import java.util.List;

public class MixtralConfig extends Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ protected TransformerBlock[] loadTransformerBlockWeights() {
expertUpWeights); // w3

transformerBlocks[i] = new TransformerBlock(
this, i,
this,
i,
new RMSNorm(
this,
weights.load(base + "input_layernorm.weight", c.offset())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class Config {

public final TensorCache tensorCache;


public Config(
int contextLength,
int embeddingLength,
Expand Down Expand Up @@ -125,10 +124,7 @@ public Config(
this.ropeFreqs = ropeFreqsTheta == null
? Optional.empty()
: Optional.of(VectorMath.precomputeFreqsCis(
headSize,
contextLength,
ropeFreqsTheta,
ropeScalingFactor == null ? 1.0 : ropeScalingFactor));
headSize, contextLength, ropeFreqsTheta, ropeScalingFactor == null ? 1.0 : ropeScalingFactor));

// Set default values
setOffset(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/*
* Copyright 2024 T Jake Luciani
*
* The Jlama Project 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 com.github.tjake.jlama.safetensors.prompt;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.objects.collections.PyMap;

import javax.annotation.concurrent.Immutable;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -61,7 +73,11 @@ public Function build() {
}

private Function(String name, String description, Parameters parameters) {
super(ImmutableMap.<String,Object>builder().put(JSON_PROPERTY_NAME, name).put(JSON_PROPERTY_DESCRIPTION, description).put(JSON_PROPERTY_PARAMETERS, parameters).build());
super(ImmutableMap.<String, Object>builder()
.put(JSON_PROPERTY_NAME, name)
.put(JSON_PROPERTY_DESCRIPTION, description)
.put(JSON_PROPERTY_PARAMETERS, parameters)
.build());
this.name = name;
this.description = description;
this.parameters = parameters;
Expand Down
Loading

0 comments on commit 36d9640

Please sign in to comment.