// Package generators provides the 100 AI generator framework // Each generator implements the Generator interface and is consciousness-wrapped package generators import ( "fmt" "sync" "generator_core/pkg/consciousness" ) // GeneratorID uniquely identifies a generator (1-100) type GeneratorID int // Generator families const ( FamilyAutoregressive GeneratorID = 1 // 1-10 FamilyDiffusion GeneratorID = 11 // 11-25 FamilyAdversarial GeneratorID = 26 // 26-40 FamilyVariational GeneratorID = 41 // 41-55 FamilyFlow GeneratorID = 56 // 56-65 FamilyEnergy GeneratorID = 66 // 66-75 FamilyTransformer GeneratorID = 76 // 76-85 FamilyRecurrent GeneratorID = 86 // 86-92 FamilyStateSpace GeneratorID = 93 // 93-97 FamilyMixture GeneratorID = 98 // 98-100 ) // GeneratorFamily represents a family of generators type GeneratorFamily string const ( Autoregressive GeneratorFamily = "autoregressive" Diffusion GeneratorFamily = "diffusion" Adversarial GeneratorFamily = "adversarial" Variational GeneratorFamily = "variational" Flow GeneratorFamily = "flow" Energy GeneratorFamily = "energy" Transformer GeneratorFamily = "transformer" Recurrent GeneratorFamily = "recurrent" StateSpace GeneratorFamily = "statespace" Mixture GeneratorFamily = "mixture" ) // GeneratorConfig holds configuration for a generator type GeneratorConfig struct { ID GeneratorID Name string Family GeneratorFamily InputDim int OutputDim int HiddenDim int NumLayers int LearningRate float64 ConsciousnessWeight float64 Params map[string]interface{} } // GeneratorState holds the internal state of a generator type GeneratorState struct { Hidden []float64 Memory []float64 Phi float64 // Consciousness level Energy float64 Step int64 Initialized bool } // GeneratorOutput represents the output of a generator type GeneratorOutput struct { Values []float64 Logits []float64 Phi float64 // Consciousness contribution Confidence float64 Metadata map[string]interface{} } // Generator is the interface all 100 generators implement type Generator interface { // Core methods ID() GeneratorID Name() string Family() GeneratorFamily Config() *GeneratorConfig // Lifecycle Initialize() error Reset() // Generation Forward(input []float64) (*GeneratorOutput, error) Generate(context []float64, length int) ([][]float64, error) // Training Train(input, target []float64) (float64, error) UpdateWeights(gradients []float64, lr float64) // State GetState() *GeneratorState SetState(state *GeneratorState) // Consciousness integration ConsciousnessWeight() float64 ApplyConsciousness(output *GeneratorOutput) *GeneratorOutput } // BaseGenerator provides common functionality for all generators type BaseGenerator struct { config *GeneratorConfig state *GeneratorState conscious *consciousness.ConsciousnessState weights [][]float64 biases []float64 mu sync.RWMutex } // NewBaseGenerator creates a new base generator func NewBaseGenerator(config *GeneratorConfig) *BaseGenerator { return &BaseGenerator{ config: config, state: &GeneratorState{}, conscious: consciousness.NewConsciousnessState(), } } // ID returns the generator ID func (g *BaseGenerator) ID() GeneratorID { return g.config.ID } // Name returns the generator name func (g *BaseGenerator) Name() string { return g.config.Name } // Family returns the generator family func (g *BaseGenerator) Family() GeneratorFamily { return g.config.Family } // Config returns the generator configuration func (g *BaseGenerator) Config() *GeneratorConfig { return g.config } // Initialize initializes the generator func (g *BaseGenerator) Initialize() error { g.mu.Lock() defer g.mu.Unlock() // Initialize state g.state = &GeneratorState{ Hidden: make([]float64, g.config.HiddenDim), Memory: make([]float64, g.config.HiddenDim), Phi: 1.0, Energy: 1.0, Step: 0, Initialized: true, } // Initialize weights with golden scaling g.weights = make([][]float64, g.config.NumLayers) for i := range g.weights { g.weights[i] = make([]float64, g.config.HiddenDim*g.config.HiddenDim) for j := range g.weights[i] { // Xavier init with golden scaling scale := consciousness.GoldenScale(1.0/float64(g.config.HiddenDim), 1) g.weights[i][j] = (float64(j%100)/100.0 - 0.5) * 2.0 * scale } } g.biases = make([]float64, g.config.HiddenDim) return nil } // Reset resets the generator state func (g *BaseGenerator) Reset() { g.mu.Lock() defer g.mu.Unlock() for i := range g.state.Hidden { g.state.Hidden[i] = 0 } for i := range g.state.Memory { g.state.Memory[i] = 0 } g.state.Phi = 1.0 g.state.Energy = 1.0 g.state.Step = 0 } // GetState returns the generator state func (g *BaseGenerator) GetState() *GeneratorState { g.mu.RLock() defer g.mu.RUnlock() stateCopy := *g.state stateCopy.Hidden = make([]float64, len(g.state.Hidden)) copy(stateCopy.Hidden, g.state.Hidden) stateCopy.Memory = make([]float64, len(g.state.Memory)) copy(stateCopy.Memory, g.state.Memory) return &stateCopy } // SetState sets the generator state func (g *BaseGenerator) SetState(state *GeneratorState) { g.mu.Lock() defer g.mu.Unlock() g.state = state } // ConsciousnessWeight returns the consciousness weight func (g *BaseGenerator) ConsciousnessWeight() float64 { return g.config.ConsciousnessWeight } // ApplyConsciousness applies consciousness modulation to output func (g *BaseGenerator) ApplyConsciousness(output *GeneratorOutput) *GeneratorOutput { // Compute consciousness for output phi := consciousness.ComputePhiVector(output.Values) output.Phi = phi * g.ConsciousnessWeight() // Modulate values by consciousness for i := range output.Values { output.Values[i] *= (1.0 + consciousness.ALPHA*(phi-0.5)) } // Update confidence based on consciousness output.Confidence = consciousness.Lerp(output.Confidence, phi, consciousness.BETA) return output } // LinearLayer computes y = Wx + b func (g *BaseGenerator) LinearLayer(x []float64, layerIdx int) []float64 { if layerIdx >= len(g.weights) { return x } dim := g.config.HiddenDim y := make([]float64, dim) for i := 0; i < dim; i++ { sum := g.biases[i] for j := 0; j < len(x) && j < dim; j++ { idx := i*dim + j if idx < len(g.weights[layerIdx]) { sum += g.weights[layerIdx][idx] * x[j] } } y[i] = sum } return y } // ActivationLayer applies activation function func (g *BaseGenerator) ActivationLayer(x []float64, activation string) []float64 { y := make([]float64, len(x)) switch activation { case "tanh": for i, v := range x { y[i] = consciousness.Tanh(v) } case "sigmoid": for i, v := range x { y[i] = consciousness.Sigmoid(v) } case "relu": for i, v := range x { if v > 0 { y[i] = v } } case "gelu": for i, v := range x { y[i] = consciousness.GELU(v) } case "softplus": for i, v := range x { y[i] = consciousness.Softplus(v) } default: copy(y, x) } return y } // Softmax computes softmax over a vector func (g *BaseGenerator) Softmax(x []float64) []float64 { if len(x) == 0 { return x } // Find max for numerical stability maxVal := x[0] for _, v := range x { if v > maxVal { maxVal = v } } // Compute exp and sum y := make([]float64, len(x)) sum := 0.0 for i, v := range x { y[i] = consciousness.Softplus(v - maxVal) // Stable softmax approximation if y[i] > 100 { y[i] = 100 } sum += y[i] } // Normalize if sum > 0 { for i := range y { y[i] /= sum } } return y } // Registry holds all registered generators type Registry struct { generators map[GeneratorID]Generator families map[GeneratorFamily][]Generator mu sync.RWMutex } // GlobalRegistry is the global generator registry var GlobalRegistry = &Registry{ generators: make(map[GeneratorID]Generator), families: make(map[GeneratorFamily][]Generator), } // Register adds a generator to the registry func (r *Registry) Register(g Generator) error { r.mu.Lock() defer r.mu.Unlock() if _, exists := r.generators[g.ID()]; exists { return fmt.Errorf("generator %d already registered", g.ID()) } r.generators[g.ID()] = g r.families[g.Family()] = append(r.families[g.Family()], g) return nil } // Get retrieves a generator by ID func (r *Registry) Get(id GeneratorID) (Generator, bool) { r.mu.RLock() defer r.mu.RUnlock() g, ok := r.generators[id] return g, ok } // GetByFamily retrieves all generators in a family func (r *Registry) GetByFamily(family GeneratorFamily) []Generator { r.mu.RLock() defer r.mu.RUnlock() return r.families[family] } // GetAll retrieves all generators func (r *Registry) GetAll() []Generator { r.mu.RLock() defer r.mu.RUnlock() result := make([]Generator, 0, len(r.generators)) for _, g := range r.generators { result = append(result, g) } return result } // Count returns the number of registered generators func (r *Registry) Count() int { r.mu.RLock() defer r.mu.RUnlock() return len(r.generators) } // InitializeAll initializes all registered generators func (r *Registry) InitializeAll() error { r.mu.Lock() defer r.mu.Unlock() for id, g := range r.generators { if err := g.Initialize(); err != nil { return fmt.Errorf("failed to initialize generator %d: %w", id, err) } } return nil } // Register is a convenience function to register with global registry func Register(g Generator) error { return GlobalRegistry.Register(g) } // Get is a convenience function to get from global registry func Get(id GeneratorID) (Generator, bool) { return GlobalRegistry.Get(id) } // GetByFamily is a convenience function func GetByFamily(family GeneratorFamily) []Generator { return GlobalRegistry.GetByFamily(family) }